Ext.define('wmf.controller.ec.ec', {
    extend: 'Ext.app.Controller',
    stores: ['ec.ec',
             'ec.province',
             'ec.paymentType'
             //'ec.ecName'
             ],
    models: ['ec.ec'],
    views: ['ec.main', 
            'ec.list',
            'ec.add',
            'ec.edit',
            'ec.query',
            'ec.east'],
    
    refs: [{
        ref: 'ecList',
        selector: 'ecList'
    },{
        ref: 'ecMain',
        selector: 'ecMain'
    },{
        ref: 'ecQuery',
        selector: 'ecQuery'
    },{
        ref: 'ecAdd',
        selector: 'ecAdd'
    },{
        ref: 'ecEdit',
        selector: 'ecEdit'
	},{
        ref: 'ecEast',
        selector: 'ecEast'
    }],
    
    init: function () {
        this.control({
        	'ecList': {
        		beforerender : this.onBeforeRender,
        		celldblclick: this.onCellDblClick
            },
            'ecList button#add': {
                click: this.onAddButtonClick
            },
            'ecList button#query': {
                click: this.onQueryButtonClick
            },
            'ecList button#export': {
                click: this.onExportButtonClick
            },
			'ecEast tool#minimize': {
				click: this.onMinToolClick
			},
			'ecAdd button#save': {
				click: this.onAddSaveButtonClick
			},
			'ecAdd button#reset': {
				click: this.onAddResetButtonClick
			},
			'ecAdd button#cancel': {
				click: this.onMinToolClick
			},
			'ecEdit button#save': {
				click: this.onEditSaveButtonClick
			},
			'ecEdit button#reset': {
				click: this.onEditResetButtonClick
			},
			'ecEdit button#cancel': {
				click: this.onMinToolClick
			},
        	'ecQuery button#save': {
				click: this.onQuerySaveButtonClick
			},
			'ecQuery button#reset': {
				click: this.onQueryResetButtonClick
			},
			'ecQuery button#cancel': {
				click: this.onMinToolClick
			}
        });
        wmf.ec = {};
        wmf.ec.params = {};
        wmf.ec.data = {};
        wmf.ec.init = true;
    },

    onAddButtonClick: function(){
    	var win = this.getEcEast();
        win.removeAll();
        win.add(Ext.widget("ecAdd"));
        win.show();
    },
    
    onCellDblClick: function(list, td, cellIndex, record, tr, rowIndex, e, eOpts){
    	var me = this;
    	var win = me.getEcEast();
        win.removeAll();
        win.add(Ext.widget("ecEdit"));
        win.show();
        Ext.Ajax.request({
			url:'ec/getEcDetail.do',
			params: {
				id: record.data.id
			},
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				wmf.ec.data = responds.single;
				me.getEcEdit().getForm().setValues(wmf.ec.data);
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onQueryButtonClick: function(){
    	var win = this.getEcEast();
        win.removeAll();
        win.add(Ext.widget("ecQuery"));
        win.show();
        this.getEcQuery().getForm().setValues(wmf.ec.params);
    },
    
    onExportButtonClick: function(){
    	var id = wmf.ec.params.id ? wmf.ec.params.id : '';
		var ecName = wmf.ec.params.ecName ? wmf.ec.params.ecName  : '';
		var province = wmf.ec.params.province ? wmf.ec.params.province : '';
		var constractBeginMonthStart = wmf.ec.params.constractBeginMonthStart ? wmf.ec.params.constractBeginMonthStart : '';
		var constractBeginMonthEnd = wmf.ec.params.constractBeginMonthEnd ? wmf.ec.params.constractBeginMonthEnd : '';
		var constractEndMonthStart = wmf.ec.params.constractEndMonthStart ? wmf.ec.params.constractEndMonthStart : '';
		var constractEndMonthEnd = wmf.ec.params.constractEndMonthEnd ? wmf.ec.params.constractEndMonthEnd : '';
		window.location.href='ec/exportEcList.do?id='
			+encodeURI(id)+'&ecName='
			+encodeURI(ecName)+'&province='
			+encodeURI(province)+'&constractBeginMonthStart='
			+encodeURI(constractBeginMonthStart)+'&constractBeginMonthEnd='
			+encodeURI(constractBeginMonthEnd)+'&constractEndMonthStart='
			+encodeURI(constractEndMonthStart)+'&constractEndMonthEnd='
			+encodeURI(constractEndMonthEnd);
    },
    
    onAddResetButtonClick: function(){
    	this.getEcAdd().getForm().reset();
    },
    
	onAddSaveButtonClick: function(){
    	var me = this;
    	var former = this.getEcAdd();
    	var value = former.getValues();
		var win = this.getEcEast();
		Ext.Ajax.request({
			url:'ec/saveEc.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getEcList().getStore().reload();
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onEditResetButtonClick: function(){
    	this.getEcEdit().getForm().setValues(wmf.ec.data);
    },
    
    onEditSaveButtonClick: function(){
    	var me = this;
    	var former = this.getEcEdit();
    	var value = former.getValues();
		var win = this.getEcEast();
		Ext.Ajax.request({
			url:'ec/saveEc.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getEcList().getStore().reload();
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onQueryResetButtonClick: function(){
    	var form = this.getEcQuery().getForm();
    	form.reset();
    },
    
	onQuerySaveButtonClick: function(){
    	var me = this;
    	var former = this.getEcQuery();
    	var value = former.getValues();
		var win = this.getEcEast();
		wmf.ec.init = true;
		wmf.ec.params = value;
		win.hide();
		me.getEcList().getStore().pagebar.getStore().currentPage = 1;
		me.getEcList().getStore().reload();
    },
	
	onMinToolClick: function(){
    	var win = this.getEcEast();
    	win.hide();
    },
    
    onBeforeRender: function(component,options){
    	wmf.data.loadAction(this.getEcMain().itemId, this.getEcList());
    	wmf.ec.params = {};
    	component.getStore().load();
    }
});